﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.FloatWindows
{
    public partial class FloatWindowsEdit : PageBase
    {
        #region 页面属性
        public string Category
        {
            get
            {
                return this.Request["category"] ?? "FloatWindow";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyFloatWindow model = DB.FloatWindows.Find(id);
                if (model == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = model.Title;
                cbStatus.Checked = model.ShowInPage;
                txtUrl.Text = model.ContentUrl;
                this.rbtype.SelectedValue = model.ContentType.ToString();
                this.rbposition.SelectedValue = model.Position;
                heContent.Text = model.Content;
                hdImageId.Text = model.ImgUrl;
                if (!string.IsNullOrEmpty(hdImageId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdImageId.Text);
                    HyperLink1.ImageID = hdImageId.Text;
                }
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyFloatWindow model = DB.FloatWindows.Find(id);
                if (model == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                model.Title = txtTitle.Text.Trim();
                model.ShowInPage = cbStatus.Checked;
                model.Content = heContent.Text;
                model.Position = this.rbposition.SelectedValue;
                model.ContentType = Convert.ToInt32(string.IsNullOrEmpty(this.rbtype.SelectedValue) ? "1" : this.rbtype.SelectedValue);
                model.ContentUrl = txtUrl.Text;
                model.ImgUrl = hdImageId.Text;
                model.ModifyTime = DateTime.Now;
                model.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyFloatWindow model = new PartyFloatWindow();
                model.ID = StringHelper.NewID;
                model.Title = txtTitle.Text.Trim();
                model.ShowInPage = cbStatus.Checked;
                model.Content = heContent.Text;
                model.Position = this.rbposition.SelectedValue;
                model.ContentType = Convert.ToInt32(string.IsNullOrEmpty(this.rbtype.SelectedValue) ? "1" : this.rbtype.SelectedValue);
                model.ContentUrl = txtUrl.Text;
                model.ImgUrl = hdImageId.Text;
                model.CreateTime = DateTime.Now;
                model.ModifyTime = DateTime.Now;
                model.CreateBy = CurrentUser.LoginID;
                model.ModifyBy = CurrentUser.LoginID;

                DB.FloatWindows.Add(model);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }
        #endregion
    }
}